/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package graphics;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.VolatileImage;
import java.util.ArrayList;
import stealth.Main;

/**
 *
 * @author Joshua
 */
public class NonConcurrentRenderer extends AbstractRenderer {

    private VolatileImage background;

    public NonConcurrentRenderer() {
        renderingObjects = new ArrayList<RenderInstruction>();
    }
    private ArrayList<RenderInstruction> renderingObjects;

    @Override
    public void addRenderInstruction(RenderInstruction renderOp) {
        renderingObjects.add(renderOp);
    }

    @Override
    public void renderAll(Graphics2D g) {
        g.clearRect(0, 0, Main.game.getWidth(), Main.game.getHeight());
        if (background != null) {
            for (int i = 0;(double) i < (double)Main.game.getWidth() / background.getWidth(); i++) {
                for (int j = 0; (double)j< (double)Main.game.getHeight() / background.getHeight(); j++) {
                    g.drawImage(background, i*background.getWidth(), j*background.getHeight()+60,null);
                }
            }
        }
        for (RenderInstruction renderOp : renderingObjects) {
            renderOp.render(g);
        }
        renderingObjects.clear();
    }

    @Override
    public boolean finishedRendering() {
        return true;
    }

    @Override
    public void setBackgroundImage(Image i) {
        if (i == null) {
            return;
        }
        /**
         * TODO: change to load a small image and tile it similar to the blockade class
         * don't forget to change ThreadedRenderer
         */
        if (background == null) {
            background = Images.createVolatileImage(i.getWidth(null), i.getHeight(null), Transparency.OPAQUE);
        }
        Graphics2D g = (Graphics2D) background.getGraphics();
        g.clearRect(0, 0, Main.game.getWidth(), Main.game.getWidth());
        //g.drawImage(i.getScaledInstance(Main.game.getWidth(), Main.game.getHeight(), Image.SCALE_SMOOTH), 0, 0, null);
        g.drawImage(i,0,0,null);
        g.dispose();
    }
}
